knitr::opts_chunk$set(echo = TRUE)

Introduction

PranaGEO R API engine is a scalable, secure, stateless REST API alowing to make R calls. It is possible to run code that is offered by standart R or in 3rd party R packages that can be easily installed from CRAN, GitHub or similar sources.

HTML rendering of this document will be deployed online at http://coreos3.pranageo.com/ocpu/apps/pranageo/graphileonR/www/R_API_Examples.html

library(httr)
library(jsonlite)

Input data

The input data for this demo is accessible (online)[https://github.com/PranaGeo/graphileonR/blob/master/inst/extdata/data.json]. One can download it directly with curl :

```{bash results="hide"} curl -O https://raw.githubusercontent.com/PranaGeo/graphileonR/master/inst/extdata/data.json

or use the getter function *grr_sampledata()*

```{bash results="hide"}
curl -X POST http://coreos3.pranageo.com/ocpu/apps/pranageo/graphileonR/R/grr_sampledata/print

We can also download it using R and convert it to data.frame

res <- POST("http://coreos3.pranageo.com/ocpu/apps/pranageo/graphileonR/R/grr_sampledata/print")
df <- jsonlite::fromJSON(rawToChar(res$content))
df

Multiple regression

To study the factors affecting the sleep, we could run a multiple linear regression on the dataset. The following analysis reveals how much the time spent at work/family/shopping is affecting the hours spent in sleep.

model <- lm(sleep ~ work + family + shopping, data = df)
summary(model)

Calculating mean values

Using curl

curl -X POST -s http://coreos3.pranageo.com/ocpu/apps/pranageo/graphileonR/R/grr_meananalysis/print -F sampledata=@"data.json"

Using R

res <- POST("http://coreos3.pranageo.com/ocpu/apps/pranageo/graphileonR/R/grr_meananalysis/json", body = list(sampledata=upload_file("../extdata/data.json")))
data <- fromJSON(fromJSON(rawToChar(res$content)))
df.avg <- data.frame(hobby=names(data), averages=sapply(data, function(x)x))
df.avg

Plotting results in a pie chart

suppressPackageStartupMessages(library(plotly))
plot_ly(data=df.avg, labels=~hobby, values=~averages, type = "pie")


PranaGeo/graphileonR documentation built on May 31, 2019, 2:21 p.m.